package com.converse.diamondreader.activities.findId;

import android.Manifest;
import android.content.Context;
import android.graphics.Rect;
import android.hardware.camera2.CameraCharacteristics;
import android.os.Bundle;
import android.util.Pair;
import android.view.Display;

import com.converse.diamondreader.R;
import com.converse.diamondreader.base.BaseUiActivity;
import com.converse.diamondreader.customized.FrameGraphic;
import com.converse.diamondreader.customized.GraphicOverlay;
import com.converse.diamondreader.customized.RecognizeTextView;
import com.converse.diamondreader.customized.TextGraphic;
import com.converse.diamondreader.customized.TrackBoxLayerView;
import com.google.mlkit.vision.text.Text;

import java.util.List;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.camera.view.PreviewView;
import androidx.lifecycle.LifecycleOwner;

/**
 * https://medium.com/swlh/introduction-to-androids-camerax-with-java-ca384c522c5
 *
 * https://developers.google.com/ml-kit/vision/text-recognition/v2/android
 *
 * https://github.com/googlesamples/mlkit
 */
public class FindIdActivity extends BaseUiActivity implements FindIdView {

    private PreviewView mPreviewView;
    private AppCompatTextView mTvDiamondId;
    private FindIdPresenter mPresenter;
    private AppCompatTextView mTvVersion;
    private GraphicOverlay mLayerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_id);
        mPreviewView = findViewById(R.id.activity_find_id_preview_view);
        mTvDiamondId = findViewById(R.id.activity_find_id_tv_diamond_id);
        mTvVersion = findViewById(R.id.activity_find_id_tv_version);
        mLayerView = findViewById(R.id.activity_find_id_track_box_layer_view);
        mLayerView.setCameraInfo(mPreviewView.getWidth(), mPreviewView.getHeight(), CameraCharacteristics.LENS_FACING_BACK);
        mPresenter = new FindIdPresenter(this, new FindIdModelImpl());
        mPresenter.displayVersion();

    }



    @Override
    public String[] requestPermissions() {
        return new String[]{
                Manifest.permission.CAMERA,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
        };
    }

    @Override
    protected void onPermissionFulfilled(String[] permissions) {
        mPresenter.startPreview();
    }


    @Override
    public PreviewView getViewFinder() {
        return mPreviewView;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void onGetId(String msg) {
//        if(TextUtils.isEmpty(msg)){
//            msg="";
//        }
//        mTvDiamondId.setText(msg);
    }

    @Override
    public LifecycleOwner getLifeCycleOwner() {
        return this;
    }

    @Override
    public Display getWindowDisplay() {
        return getWindowManager().getDefaultDisplay();
    }

    @Override
    public void showVersionCode(String version) {
        mTvVersion.setText("Ver "+version);
    }

    @Override
    public void showToast(String msg) {
        super.showToast(msg);
    }

    @Override
    public void onGetIdAndTextFrame(String id, Rect frame) {
//        onGetId(id);
//        mLayerView.drawHighLightText(id,frame);
    }

    @Override
    public void onGetRecognizeTexts(List<Text.Line> recognizedTexts) {
        if(recognizedTexts.isEmpty()){
            return;
        }
        mLayerView.clear();
//        for (Pair<String, Rect> pair : recognizedTexts) {
//            RecognizeTextView view = new RecognizeTextView(pair.first, pair.second);
//            mLayerView.addRecognizeView(view);
//        }
        for (Text.Line line : recognizedTexts) {
            TextGraphic graphic = new TextGraphic(mLayerView,line);
            FrameGraphic frameGraphic = new FrameGraphic(mLayerView, line);
//            mLayerView.add(graphic);
            mLayerView.add(frameGraphic);
        }
        mLayerView.postInvalidate();
    }
}